package team.epos.utils.util;


import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

	private  static ObjectMapper objectMapper = new ObjectMapper();
	
	public static <T>String obJectToJson(T obj){
		if (obj == null) {
			return null;
		}else {
			try {
	            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        }
		}
	}
	
	public static <T>T jsonToObject(String s,Class<T> obj) {
		if(s.isEmpty()|| s==null || obj == null){
            return null;
        }
        try {
            return obj.equals(String.class) ? (T) s : objectMapper.readValue(s,obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
	}
	
	public static <T>List<T> jsonToList(String s,T obj) {
		if(s.isEmpty()|| s==null || obj == null){
            return null;
        }
        try {
            return objectMapper.readValue(s,new TypeReference<List<T>>() { });
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
	}
	
}
